setwd("C:/Users/86139/Desktop/R作业/ADdata/")
library(openxlsx)
a <- read.table("ADdata1.txt")
b <- read.csv('ADdata2.csv')
c <- read.xlsx("ADdata3.xlsx")
d <- read.table("ADdata4.txt")

colnames(b)[1] <- "ProteinID"
colnames(c)[1] <- "ProteinID"
merged_data1 <- merge(a, d, by.x=0, by.y=0, all=TRUE) 
colnames(merged_data1)[1] <- "ProteinID"
merged_data2 <- merge(b, c, by="ProteinID", all=TRUE)
merged_data <- merge(merged_data1,merged_data2,by="ProteinID")

ADdata <- merged_data[,2]
ADdata1 <- as.matrix(ADdata)
hist(ADdata1)
hist(log2(ADdata1))

#使用欧氏距离算法 聚类
dist_matrix <- dist(merged_data, method = "euclidean")
hclust_result <- hclust(dist_matrix, method = "complete")
plot(hclust_result, labels = rownames(merged_data), hang = 0.1, cex = 0.5)


library(ggplot2)
library(dplyr)
# 导入数据
load("C:/Users/86139/Desktop/R作业/volcano.RData") 
volcano_data <- mutate(prostat, neg_log10_P = -log10(P))
# 提取上调蛋白和下调蛋白的数据
upregulated <- filter(volcano_data,P < 0.05 & FC > log2(1.2))
downregulated <- filter(volcano_data,P < 0.05 & FC < log2(1/1.2))
# 绘制火山图
p <- ggplot(volcano_data, aes(x = FC, y = neg_log10_P)) +
  geom_point(color = "grey", size = 0.8) +
  geom_point(data = upregulated, aes(color = "up"), size = 0.8) +
  geom_point(data = downregulated, aes(color = "down"), size = 0.8) +
  scale_color_manual(values = c("up" = "red", "down" = "blue"), guide = FALSE) +
  labs(x = "Log2 Fold Change", y = "-Log10 P-value", title = "Volcano Plot") +
  theme_minimal()+
  guides(color = guide_legend(title = "Regulation", override.aes = list(shape = c(20, 20), size = c(2, 2), color = c("red", "blue"))))+
  geom_vline(xintercept = c(log2(1.2), log2(1/1.2)), linetype = "dashed", color = "black") +  # 添加垂直辅助线
  geom_hline(yintercept = -log10(0.05), linetype = "dashed", color = "black")  # 添加水平辅助线
plot(p)+theme(plot.title = element_text(hjust = 0.5))
# 输出为jpg文件
ggsave("volcano_plot.jpg", plot = p, width = 10, height = 6, units = "in", dpi = 300)


